package com.igoal.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;

@Entity
@Table(name = "user_field")
@Searchable
public class UserInfo extends BaseObject implements Serializable, Audit {

	private static final long serialVersionUID = 700907990676914744L;
	private Long id;
	private String firstName;
	private String middleName;
	private String lastName;
	private Date birthday;
	private String mobilePhone;
	private String phone;
	private String address;
	private String district;
	private City city;
	private DataStatus status;
	private Date createDate;
	private Date updateDate;
	private User user;
	private String yahoo;
	private String google;
	private String skype;
	private String aboutMe;
	private String facebook;
	private String website;
	private boolean male;
	
	@OneToOne(fetch = FetchType.LAZY)
	@PrimaryKeyJoinColumn
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	@GeneratedValue(generator = "system-foreign")
	@GenericGenerator(name = "system-foreign", 
			strategy = "foreign",
            parameters = @Parameter(name = "property", value = "user"))
	@Id
    @SearchableId    
    @Column(name = "id", unique = true, nullable = false)
    public Long getId() {
        return id;
    }
	
	public void setId(Long id){
		this.id = id;
	}	

	public UserInfo() {}
	
	@Column(name = "about_me", nullable = true)	
	public String getAboutMe() {
		return aboutMe;
	}

	public void setAboutMe(String aboutMe) {
		this.aboutMe = aboutMe;
	}

	@Column(nullable = true, name = "first_name")
	@SearchableProperty
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firtName) {
		this.firstName = firtName;
	}

	@Column(nullable = true, name = "middle_name")
	@SearchableProperty
	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}
	
	@Column(nullable = true, name = "last_name")
	@SearchableProperty
	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	@Column(nullable = false, name = "DOB")
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	@Column(name = "sex", nullable = false)
	@Type(type = "yes_no")
	public boolean isMale() {
		return male;
	}

	public void setMale(boolean isMale) {
		this.male = isMale;
	}
	
	@Column(nullable = true, name = "mobile")
	public String getMobile() {
		return mobilePhone;
	}

	public void setMobile(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}
	
	@Column(nullable = true, name = "phone")
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	@Column(nullable = true, name = "address")
	@SearchableProperty
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Column(name="district", nullable = true)
	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "city_id")
	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}
	
	@Column(name = "yahoo", length = 50, nullable=true)
	public String getYahoo() {
		return this.yahoo;
	}

	public void setYahoo(String yahoo) {
		this.yahoo = yahoo;
	}

	@Column(name = "google", length = 50, nullable=true)
	public String getGoogle() {
		return this.google;
	}

	public void setGoogle(String google) {
		this.google = google;
	}

	@Column(name = "skype", length = 50, nullable=true)
	public String getSkype() {
		return this.skype;
	}

	public void setSkype(String skype) {
		this.skype = skype;
	}
	
	@Column(name="facebook", length = 50, nullable=true)
	public String getFacebook() {
		return facebook;
	}

	public void setFacebook(String facebook) {
		this.facebook = facebook;
	}

	@Column(name="website", nullable=true)
	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable = true, name = "status")
	@SearchableProperty
	public DataStatus getStatus() {
		return status;
	}

	public void setStatus(DataStatus dataStatus) {
		this.status = dataStatus;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, name = "cdate")
	public Date getCreateDate() {
		return createDate;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false, name = "mdate")
	public Date getUpdateDate() {
		return updateDate;		
	}
	
	public void setCreateDate(Date date) {
		this.createDate = date;
	}
	
	public void setUpdateDate(Date date) {
		this.updateDate = date;
	}
	
	@Override
	public boolean equals(Object o) {
		boolean result = true;
		if(o instanceof UserInfo){
			UserInfo target = (UserInfo) o;
			if(this.firstName != null && target.firstName != null){
				result &= this.firstName.equals(target.firstName);
			}else{
				return false;
			}
			if(this.middleName != null && target.middleName != null){
				result &= this.middleName.equals(target.middleName);
			}else{
				return false;
			}
			if(this.lastName != null && target.lastName != null){
				result &= this.lastName.equals(target.lastName);
			}else{
				return false;
			}
			if(this.birthday != null && target.birthday != null){
				result &= this.birthday.equals(target.birthday);
			}else{
				return false;
			}			
		}
		return result;
	}

	@Override
	public int hashCode() {
		return this.birthday != null ? this.birthday.hashCode() : 0;
	}

	@Override
	public String toString() {
		return "Information of user " + user.getUsername();
	}	
}
